// ignore_for_file: file_names

import 'package:flutter/services.dart';

/// 只允许输入整数
class UsIntTextInputFormatter extends TextInputFormatter {
  UsIntTextInputFormatter();

  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    String value = newValue.text;
    int selectionIndex = newValue.selection.end;

    String newValueS = '';
    if (value.isNotEmpty) {
      newValueS = int.parse(value).toString();
      selectionIndex = selectionIndex - (value.length - newValueS.length);
    }
    if (selectionIndex < 0) {
      selectionIndex = 0;
    }

    return TextEditingValue(
      text: newValueS,
      selection: TextSelection.collapsed(offset: selectionIndex),
    );
  }
}
